<?php
require_once("databasecreds.php");

$fld=$_GET["field"];
if($fld=="") {
  //some error
} else {
  $sql=getSQL($fld);
  if($sql=="") {
    //some error
  } else {
    $conn_res =  mysql_pconnect($db_host,$db_user,$db_pass);
    if (!$conn_res) {
      die('Not connected : ' . mysql_error());
    } else {
      if(!mysql_select_db($db_database)) {
        die( "Unable to select database");
      } else {
        $result = mysql_query($sql);
         if (!$result) {
            die('Invalid query: ' . mysql_error());
        } else {
          print_result($result);    
        }
      }
    }
  }
}

function getSQL($fld) {
  $sql="";
  if ($fld=="state") {
    $sql="select distinct(state) from mindless_psych.region;";
  } else if ($fld=="shockType") {
    $sql="select distinct(name) from mindless_psych.shockType;";
  } else if ($fld=="regionType") {
    $sql="select distinct(type) from mindless_psych.region;";
  } else if ($fld=="regionName") {
    $sql="select distinct(name) from mindless_psych.region;";
  } else if ($fld=="measurementType") {
    $sql="select distinct(name) from mindless_psych.measurementType;";
  } else if ($fld=="metric") {
    $sql="select distinct(description) from mindless_psych.metric;";
  }
  else {
    $sql="error";
  }
  return($sql);
}

function print_result($result) {
    while ($row = mysql_fetch_array($result)) {
      print($row[0] . "|");
    }
}

?>